package utils

import (
	"encoding/json"
	"io/ioutil"
	"os"
)

func ReadConfig(config ...interface{}) {
	var r *os.File
	if len(config) > 1 {
		filepath, _ := config[0].(string)
		r, _ = os.Open(filepath)
		defer r.Close()
		bs, _ := ioutil.ReadAll(r)
		json.Unmarshal(bs, config[1])
	} else {
		r, _ = os.Open("./config.json")
		defer r.Close()
		bs, _ := ioutil.ReadAll(r)
		json.Unmarshal(bs, config[0])
	}
}

//程序修改SysConfig配置表后，调用写入配置文件
func WriteSysConfig(config ...interface{}) {
	var r *os.File
	var configobj interface{}
	if len(config) > 1 {
		filepath, _ := config[0].(string)
		r, _ = os.OpenFile(filepath, os.O_WRONLY|os.O_TRUNC, 0x644)
		configobj = config[1]
	} else {
		r, _ = os.OpenFile("./config.json", os.O_WRONLY|os.O_TRUNC, 0x644)
		configobj = config[0]
	}
	defer r.Close()
	if s, ok := configobj.(string); ok {
		r.Write([]byte(s))

	} else {
		bs, _ := json.Marshal(configobj)
		r.Write(bs)
	}
}
